package com.ayoka.parenting.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import java.io.IOException;

/**
 * 定位的Service类，用户在运动时此服务会在后台进行定位。
 * Created by liuheng on 16/7/24.
 */
public class BackgroundMusicService extends Service implements MediaPlayer.OnPreparedListener {

    public static final float DEFAULT_VOLUME = 0.5f;

    public static String TAG;

    public static final String BACKGROUND_MUSIC_STAR = "BACKGROUND_MUSIC_STAR";
    public static final String BACKGROUND_MUSIC_STOP = "BACKGROUND_MUSIC_STOP";

    protected MediaPlayer mediaPlayer;

    private float volume = DEFAULT_VOLUME;

    public Binder mBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        TAG = getClass().getSimpleName();
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        try {
            mediaPlayer = new MediaPlayer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startPlay(String url) {
        try {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            } else {
                mediaPlayer.reset();
            }
            mediaPlayer.setDataSource(url); // 设置数据源
            mediaPlayer.setLooping(true);
            mediaPlayer.setVolume(DEFAULT_VOLUME, DEFAULT_VOLUME);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.prepareAsync(); // prepare自动播放
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.setVolume(volume, volume);
                mediaPlayer.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        try {
            if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        try {
            if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void release() {
        try {
            if (mediaPlayer != null) {
                try {
                    mediaPlayer.setOnErrorListener(null);
                    mediaPlayer.setOnInfoListener(null);
                    mediaPlayer.setOnPreparedListener(null);
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setVolume(float volume) {
        try {
            this.volume = volume;
            if (mediaPlayer != null) {
                mediaPlayer.setVolume(volume, volume);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        if (mBinder == null) {
            mBinder = new BackgroundMusicBinder();
        }
        return mBinder;
    }

    @Override
    public void onDestroy() {
        release();
        super.onDestroy();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        try {
            mp.setVolume(volume, volume);
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isPlaying() {
        if (mediaPlayer == null) return false;
        return mediaPlayer.isPlaying();
    }

    public class BackgroundMusicBinder extends Binder {

        public BackgroundMusicService getService() {
            return BackgroundMusicService.this;
        }
    }
}
